package ar.fiuba.fallar2.pac_man.pildora.persist;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ar.fiuba.fallar2.pac_man.AbstractDataMapper;
import ar.fiuba.fallar2.pac_man.DBManager;
import ar.fiuba.fallar2.pac_man.IBusinessObject;
import ar.fiuba.fallar2.pac_man.pildora.model.Pildora;

public class PildoraDataMapper extends AbstractDataMapper{

	@Override
	public List findAll() 
	{
		try {
			List<IBusinessObject> lista = new ArrayList<IBusinessObject>();
			String sqlQuery = "SELECT * FROM "+getTable();
			ResultSet r = DBManager.getInstance().find(sqlQuery).getResultSet();
			while (r.next()) 
				lista.add(createBO(r));
			
			r.getStatement().close();
			return lista;
		 } catch (SQLException e) {
			 e.printStackTrace();
			throw new RuntimeException(e);
		}
	
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(IBusinessObject obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void save(IBusinessObject obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public IBusinessObject load(int id) {
		// TODO Auto-generated method stub
		return null;
	}
	

@Override
protected IBusinessObject createBO(ResultSet r) throws  SQLException {
	
	 Pildora pildora= new Pildora(r.getInt("PL_id"),
			 				r.getInt("PL_Color"),
			 			    r.getInt("PL_Duracion"));
			 				  		  	
	return  pildora;
}

@Override
protected String getIdColumn() {
	// TODO Auto-generated method stub
	return "PL_id";
}

@Override
protected String getTable() {
	// TODO Auto-generated method stub
	return "Pildora";
}

@Override
protected String mapBOInsert(IBusinessObject r) throws SQLException {
	Pildora pildora = (Pildora) r;
	StringBuffer sb = new StringBuffer();
	sb.append("INSERT INTO " + getTable());
	sb.append("( PL_Duracion  ,FT_Color ) VALUES (");
	
	sb.append(pildora.getDuracion()+", '");
	sb.append(pildora.getColor()+"') ");
	return sb.toString();
}

@Override
protected String mapBOUpdate(IBusinessObject r) throws SQLException {

	Pildora pil   = (Pildora) r;
	StringBuffer sb = new StringBuffer();
	sb.append("UPDATE " + getTable());

	sb.append(" PL_Color='"+pil.getColor());
	sb.append(", PL_Duracion='"+pil.getDuracion());
	sb.append("' WHERE PL_Id="+pil.getId()+")");
	return sb.toString();
}
	

}
